// 封装数据库的操作方法  并导出
const mysql = require('mysql');

module.exports = (sql, values = null) => {   // function db(){}
    return new Promise((resolve, reject) => {
        // 封装异步代码 resolve接收成功的结果，reject接收失败的结果
        const conn = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'root',
            database: 'big-event',
            timezone: '+HH:MM'
        });
        conn.connect();
        conn.query(sql, values, (err, result) => {
            err ? reject(err) : resolve(result);
        });
        conn.end();
    }).catch(err => {
        console.log('错误信息为：' + err.sqlMessage);
        console.log('错误SQL为：' + err.sql);
    });
}